package com.xnyzc.lhy.order.service.impl.score;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.order.entity.comment.OaSysComment;
import com.xnyzc.lhy.order.entity.order.OaDOrder;
import com.xnyzc.lhy.order.entity.order.OaOrderEvaluation;
import com.xnyzc.lhy.order.entity.work.OaDWorkStatistics;
import com.xnyzc.lhy.order.mapper.comment.OaSysCommentMapper;
import com.xnyzc.lhy.order.mapper.order.OaDOrderMapper;
import com.xnyzc.lhy.order.mapper.order.OaOrderEvaluationMapper;
import com.xnyzc.lhy.order.service.score.IOaDServiceScoreService;
import com.xnyzc.lhy.order.service.work.IOaDWorkStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 服务分
 * @Author zhuyubin
 * @Date 2019/8/5 16:24
 **/
@Service
public class OaDServiceScoreServiceImpl implements IOaDServiceScoreService {

    @Autowired
    private IOaDWorkStatisticsService iOaDWorkStatisticsService;
    @Autowired
    private OaDOrderMapper oaDOrderMapper;
    @Autowired
    private OaOrderEvaluationMapper oaOrderEvaluationMapper;
    @Autowired
    private OaSysCommentMapper oaSysCommentMapper;

    /**
     * 最小订单数
     */
    private static final int MIN_ORDER_QUANTITY = 20;

    /**
     * 小于20订单数服务描述
     */
    private static final String SERVICE_SCORE_MESSAGE = "正在【试用期】，20单内派单有限不变";

    /**
     * 服务分页面数据
     * @param driverId 司机id
     * @param tag 租户
     * @param userType 用户类型
     * @return
     */
    @Override
    public Map<String, Object> getComment(Long driverId, String tag, String userType){
        Map<String, Object> map = this.initMap();
        // 当天工作记录

        OaDWorkStatistics oaDWorkStatistics = iOaDWorkStatisticsService.getWork(driverId, tag);
        if (CheckUtil.objIsEmpty(oaDWorkStatistics)){
            return map;
        }
        map.put("serviceScore", oaDWorkStatistics.getTotalServiceScore());
        // 查询订单数量
        Integer count = oaDOrderMapper.selectCount(
                Wrappers.<OaDOrder>lambdaQuery()
                        .eq(OaDOrder::getDriverId, driverId)
                        .eq(OaDOrder::getTag, tag)
        );
        if (count != null && count <= MIN_ORDER_QUANTITY){
            map.put("serviceScoreMessage", SERVICE_SCORE_MESSAGE);
        }
        // 评价id列表
        List list = new ArrayList();
        // 评价表
        List<OaOrderEvaluation> oaOrderEvaluations = oaOrderEvaluationMapper.selectList(
                Wrappers.<OaOrderEvaluation>lambdaQuery()
                        .eq(OaOrderEvaluation::getUserId, driverId)
                        .eq(OaOrderEvaluation::getUserType, userType)
                        .orderByDesc(OaOrderEvaluation::getCreateTime)
        );
        for (OaOrderEvaluation oaOrderEvaluation : oaOrderEvaluations) {
            String tagCommentContent = oaOrderEvaluation.getTagCommentContent();
            if(StringUtils.isEmpty(tagCommentContent)){
                continue;
            }
            if (tagCommentContent.indexOf(',') != -1){
                String[] split = tagCommentContent.split(",");
                List<String> splitList = Arrays.asList(split);
                list.removeAll(splitList);
                list.addAll(splitList);
                if (list.size() <= 6){
                    continue;
                }
            }
        }
        if (list.size() <= 0){
            return map;
        }
        // 评价配置表
        List<OaSysComment> oaSysComments = oaSysCommentMapper.selectList(
                Wrappers.<OaSysComment>lambdaQuery()
                        .eq(OaSysComment::getTag, tag)
                        .in(OaSysComment::getCommentId, list)
        );
        // 加分列表
        List awardedMarksList = new ArrayList();
        // 减分列表
        List deductionList = new ArrayList();
        for (OaSysComment oaSysComment : oaSysComments) {
            if (oaSysComment.getCommentGradeType() == 0){
                awardedMarksList.add(oaSysComment.getCommentContent());
            } else {
                deductionList.add(oaSysComment.getCommentContent());
            }
        }
        map.put("awardedMarksList", awardedMarksList);
        map.put("deductionList", deductionList);
        return map;
    }

    /**
     * 初始化出参
     * @return
     */
    private Map<String, Object> initMap(){
        List initList = new ArrayList();
        Map<String, Object> map = new HashMap<>(16);
        map.put("serviceScore", "");
        map.put("serviceScoreMessage", "");
        map.put("awardedMarksList", initList);
        map.put("deductionList", initList);
        return map;
    }
}
